package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.service.DishService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 菜品管理
 */
@RestController
@RequestMapping("dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 分页查询&根据名称模糊分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("page")
    public R<Page<DishDto>> page(Integer page,Integer pageSize,String name){
        Page<DishDto> page1 = new Page<>(page,pageSize);
        Page<DishDto> page2 = dishService.findDishAndCategoryName(page1,name);
        return R.success(page2);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R<DishDto> dishDto(@PathVariable  Long id){
        DishDto byIdWithDishAndDishFlavor = dishService.findByIdWithDishAndDishFlavor(id);
        return R.success(byIdWithDishAndDishFlavor);
    }

    /**
     * 添加菜品
     * @param dishDto
     * @return
     */
    @PostMapping
    @CacheEvict(cacheNames = "dish",allEntries = true)
    public R<String> dishAdd(@RequestBody  DishDto dishDto){
//        Set keys = redisTemplate.keys("dish*");
//        redisTemplate.delete(keys);
        dishService.addDishAndDishFlavor(dishDto);
        return R.success("添加成功");
    }

    /**
     * 修改菜品信息
     * @param dishDto
     * @return
     */
    @PutMapping
    @CacheEvict(cacheNames = "dish",allEntries = true)
    public R<String> dishPut(@RequestBody  DishDto dishDto){
//        Set keys = redisTemplate.keys("dish*");
//        redisTemplate.delete(keys);
        dishService.updateDishAndDishFlavor(dishDto);
        return R.success("修改成功");
    }

    /**
     * 删除菜品信息 & 批量
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(cacheNames = "dish",allEntries = true)
    public R<String> dishDelete(Long[] ids){
//        Set keys = redisTemplate.keys("dish*");
//        redisTemplate.delete(keys);
        dishService.deletByIds(ids);
        return R.success("删除成功");
    }

    /**
     * 修改售卖状态 & 批量
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("status/{status}")
    @CacheEvict(cacheNames = "dish",allEntries = true)
    public R<String> dishUpdateStatus(@PathVariable Integer status,Long[] ids){
//        Set keys = redisTemplate.keys("dish*");
//        redisTemplate.delete(keys);
        dishService.lambdaUpdate()
                .in(ids!=null,Dish::getId,ids)
                .set(status!=null,Dish::getStatus,status)
                .update(new Dish());
        return R.success("修改成功");
    }

    /**
     * 根据id查询 名称模糊查询
     * @param dish
     * @return
     */
    @GetMapping("list")
    @Cacheable(cacheNames = "dish" ,key = "T(org.apache.commons.codec.digest.DigestUtils).md5Hex(#dish.toString())")
    public R<List<DishDto>> dishGetOne(Dish dish){
//        String redisKey = "dish_"+ DigestUtils.md5Hex(dish.toString());
//        List<DishDto> dishDtoList = (List<DishDto>)redisTemplate.opsForValue().get(redisKey);
//        if(dishDtoList!=null){
//            return R.success(dishDtoList);
//        }
        List<DishDto> list = dishService.selectByDto(dish);
//        redisTemplate.opsForValue().set(redisKey,list);
        return R.success(list);
    }

}
